package com.lc.recovery.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.app.AppFragment;
import com.lc.recovery.http.request.StorageTotalsApi;
import com.lc.recovery.http.response.StorageTotalsBean;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.ui.activity.HomeActivity;
import com.lc.recovery.ui.activity.LoginActivity;
import com.lc.recovery.ui.activity.warehousehome.OutboundGoodsActivity;
import com.lc.recovery.util.CourseUtils;
import com.lc.recovery.util.SPUtil;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class MessageWarehouseFragment extends AppFragment<HomeActivity> {
    @BindView(R.id.rv_recoverymessage)
    RecyclerView rvRecoverymessage;
    @BindView(R.id.layout_refresh)
    TwinklingRefreshLayout layoutRefresh;
    private List<StorageTotalsBean.DataBean> dataBeanList = new ArrayList<>();
    private int page = 1;

    private BaseQuickAdapter<StorageTotalsBean.DataBean, BaseViewHolder> messageWarehousedapter;

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        storage_totals(page);
        messageWarehousedapter = new BaseQuickAdapter<StorageTotalsBean.DataBean, BaseViewHolder>(R.layout.item_messagewarehouse, dataBeanList) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, StorageTotalsBean.DataBean bean) {
                baseViewHolder.setText(R.id.tv_type, bean.getShops());
                baseViewHolder.setText(R.id.tv_weight,bean.getWeights());

            }
        };

        View emptyview1 = View.inflate(getContext(), R.layout.empty_emptylayout, null);
        messageWarehousedapter.setEmptyView(emptyview1);
        rvRecoverymessage.setAdapter(messageWarehousedapter);
        messageWarehousedapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(), IntentKey.USERID, ""))) {
                    startActivity(LoginActivity.class);
                    return;
                }
                startActivity(new Intent(getContext(),OutboundGoodsActivity.class).putExtra("id",dataBeanList.get(position).getId()+"").putExtra("datas", (Serializable) dataBeanList));
            }
        });

        layoutRefresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                page = 1;
                storage_totals(page);


                refreshLayout.finishRefreshing();

            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
//                refreshLayout.finishLoadmore();
//                if (!TextUtils.isEmpty(goods_classify_id)) {



                        refreshLayout.setEnableLoadmore(false);


//                }

//         }
            }
        });

    }



    private void storage_totals(int page) {
        EasyHttp.post(this)
                .api(new StorageTotalsApi().setUser_id(SPUtil.get(getContext(), IntentKey.USERID, "")).setPage(page + "")).request(new HttpCallback<StorageTotalsBean>(this) {

            @Override
            public void onSucceed(StorageTotalsBean data) {
                if (data.getCode() == 200) {
                    dataBeanList.clear();
                    dataBeanList.addAll(data.getData());
                    messageWarehousedapter.setNewData(dataBeanList);
                    messageWarehousedapter.notifyDataSetChanged();

                }

            }

            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_messagewarehouse;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }
}
